package com.cpecc.dao.wuzi;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.cpecc.model.system.User;
import com.cpecc.model.wuzi.CaiGouSPR;

@Repository
public class CaiGouSPRDao {

	@Resource
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public void save(CaiGouSPR caiGouSPR){
		Session session=sessionFactory.getCurrentSession();
		session.save(caiGouSPR);
		session.flush();
	}
	
	public CaiGouSPR get(String id){
		return (CaiGouSPR) sessionFactory.getCurrentSession().get(CaiGouSPR.class, id);
	}
	
	public boolean delete(String id){
		String hql="delete CaiGouSPR t where t.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, id);
		return query.executeUpdate()>0;
	}
	
	public boolean deleteAllSPR(String shenQingId){
		String hql="delete CaiGouSPR t where t.shenQing.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, shenQingId);
		return query.executeUpdate()>0;
	}
	
	@SuppressWarnings("unchecked")
	public List<User> getAllShenPiR(String shenQingId){
		String hql="select t.user from CaiGouSPR t where t.shenQing.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, shenQingId);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<CaiGouSPR> list(String shenQingId){
		String hql="from CaiGouSPR t where t.shenQing.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, shenQingId);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<CaiGouSPR> getMySPList(User user){
		String hql="from CaiGouSPR t where t.user.id=? and t.shenQing.huanJie.id=102 order by t.shenQing.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, user.getId());
		return query.list();
	}
}
